package br.com.locautowebbd.veiculo;

import java.sql.*;

import br.com.locautowebbd.util.JDBCConnectionUtil;
import br.com.locautowebbd.util.RepositorioException;

public class RepositorioVeiculosJDBC implements RepositorioVeiculos {

	public void atualizar(Veiculo ve) throws VeiculoInexistenteException,
			RepositorioException {
		Statement stmt = null;
		try {
			String query = "UPDATE TB_VEICULO set " + " codigo='" + ve.getCodigo()
					+ "' " + "WHERE codigo='" + ve.getCodigo() + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			int i = stmt.executeUpdate(query);
			if (i == 0) {
				throw new VeiculoInexistenteException(000);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public void inserir(Veiculo ve) throws RepositorioException{
		Statement stmt = null;
		try {
			String query = "INSERT INTO TB_VEICULO VALUES( " + "'" + ve.getCodigo()
					+ "', " + "'" + ve.getPlaca() + "'," + ve.getFoto() + ", " + "'" + ve.getVideo() + "" +
							", " + "'" + ve.getAno() + ", " + "'" + ve.getTarifa() + ", " + "'" + ve.getAcessorio() + ")"; 
			stmt = JDBCConnectionUtil.obterConexao().createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public Veiculo procurar(int codigo) throws VeiculoInexistenteException,
			RepositorioException {
		Statement stmt = null;
		ResultSet rs = null;
		Veiculo procurado = null;
		try {
			String query = "SELECT * FROM TB_VEICULO " + "WHERE codigo='" + codigo+ "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			rs = stmt.executeQuery(query);
			if (rs.next()) {

				codigo = rs.getInt(codigo);
				String placa = rs.getString("placa");
				Blob foto = rs.getBlob("foto");
				Blob video = rs.getBlob("video");
				int ano = rs.getInt("ano");
				int tarifa = rs.getInt("tarifa");
				String acessorio = rs.getString("acessorio");


				procurado = new Veiculo(codigo,placa, foto, video,ano,tarifa,acessorio);

			} else {
				throw new VeiculoInexistenteException(codigo);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				rs.close();
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}

		return procurado;

	}

	public void remover(int codigo) throws VeiculoInexistenteException,
			RepositorioException {
		Statement stmt = null;
		try {
			String query = "DELETE FROM TB_VEICULO " + "WHERE codigo='" + codigo + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			int i = stmt.executeUpdate(query);
			if (i == 0) {
				throw new VeiculoInexistenteException(0);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public boolean existe(int codigo) throws RepositorioException {
		Statement stmt = null;
		ResultSet rs = null;
		boolean resp = false;
		try {
			String query = "SELECT * FROM TB_VEICULO " + "WHERE codigo='" + codigo + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			rs = stmt.executeQuery(query);
			if (rs.next()) {
				resp = true;
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				rs.close();
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}

		return resp;
	}
}
